import {
  FormProps,
  FormRules,
  RowProps,
  ColProps,
  FormItemProps,
  ElTooltipProps,
  InputProps,
  InputNumberProps,
  ElCascader,
  ElSelect,
  ElTree,
  ElTreeSelect,
  DatePickerProps,
  CheckboxProps,
  RadioProps,
  FormItemRule,
  SwitchProps,
} from 'element-plus'
import type { VxeUploadProps } from 'vxe-pc-ui'
import type { EllipsisProps } from 'naive-ui'
import { Ref } from 'vue'
import type { VxeGridProps, ColumnInfo } from '@/BaseComponent/STable/types/index.ts'
import {formApi as formApiScoped, watch as watchScoped, watchFormApi as watchFormApiScoped} from './watch'

export type formApi = formApiScoped
export type watchFormApi = watchFormApiScoped



type handleFn<T> = (values: Record<string, any>, formApi: formApi) => T | Promise<T | Symbol> | Symbol

export type { FormItemRule } from 'element-plus'
export type configType = Partial<ConfigMap[keyof ConfigMap]>

interface BaseFormItem extends Partial<FormItemProps> {
  field: string
  isHidden?: boolean
  hideLabel?: boolean
  formItemConfig?: Partial<FormItemProps> & { style?: AnyCss }
  tip?: string
  tipConfig?: Partial<ElTooltipProps>
  slotNames?: string[]
  eventFunction?: any
  options?: any[] | Ref<any[]>
  layout?: Partial<ColProps>
  labelEllipsis?: EllipsisProps
  isGroup?: boolean
  optionConfig?: any
  optionFunction?: any
  permission?: string | string[]
  defaultValue?: any
  render?: handleFn<Component | string>
  headerRender?: handleFn<Component | string>
  style?: Record<string, string>
  dependencies?: {
    triggerFields: string[]
    immediate?: boolean
    if?: handleFn<boolean>
    show?: handleFn<boolean>
    disabled?: handleFn<boolean>
    trigger?: handleFn<void>
    rules?: handleFn<FormItemRule[] | FormItemRule>
    required?: handleFn<boolean>
    configProps?: handleFn<configType>
    render?: handleFn<Component | string>
    value?: handleFn<any>
  },
  isEdit?: boolean;
  watch?: watchScoped
}

// 定义配置映射类型
type ConfigMap = {
  input: Partial<InputProps> & {valueType?: 'number'}
  inputNumber: Partial<InputNumberProps>
  textarea: Partial<InputProps>
  custom: any
  cascader: Partial<InstanceType<typeof ElCascader>['$props']>
  select: Partial<InstanceType<typeof ElSelect>['$props']> & {mapping?: {label: string}; props?: {label?: string; value?: string}}
  tree: Partial<InstanceType<typeof ElTree>['$props']>
  treeSelect: Partial<InstanceType<typeof ElTreeSelect>['$props']>
  datepicker: Partial<DatePickerProps> & {mapping?: {start: string; end: string};}
  checkBox: Partial<CheckboxProps> & {props?: {label?: string; value?: string}, checkAll: boolean | {label?: string}}
  radio: Partial<RadioProps> & {props?: {label?: string; value?: string}}
  switch: Partial<SwitchProps>
  upload: Partial<VxeUploadProps> & { 
    mapping?: {fileName: string} | false;
    uploadParams?: {bizType: string, [key: string]: any};
    valueMode?: 'file' | 'id';
    uploadHandle?: (res: any, uploadParams: {bizType: string, [key: string]: any}) => any
  }
}

// 定义通用的表单项接口
export interface FormItemTool<T extends keyof ConfigMap> extends BaseFormItem {
  type: T
  config?: ConfigMap[T]
}

// 定义工具函数
type ToUnionType<T extends keyof ConfigMap> = T extends any ? FormItemTool<T> : never;


// 生成联合类型
export type FormItemToolKeyOf = 
'cascader' 
| 'checkBox' 
| 'custom' | 
'datepicker' | 
'input' | 
'inputNumber' 
| 'radio' 
| 'select'
| 'textarea' 
| 'tree' 
| 'treeSelect'
| 'switch'
| 'upload'

// 定义联合类型
export type FormItem = ToUnionType<FormItemToolKeyOf>;
// export type FormItem =
//   | FormItemTool<'cascader'>
//   | FormItemTool<'checkBox'>
//   | FormItemTool<'custom'>
//   | FormItemTool<'datepicker'>
//   | FormItemTool<'input'>
//   | FormItemTool<'inputNumber'>
//   | FormItemTool<'radio'>
//   | FormItemTool<'select'>
//   | FormItemTool<'textarea'>
//   | FormItemTool<'tree'>
//   | FormItemTool<'treeSelect'>

export type FormItems = FormItem[]
export interface Props {
  tmpInfo?: any
  data?: Record<string, any>
  formItems: FormItems
  elFormConfig?: FormProps | object
  formConfig?: FormProps | object
  allDisabled?: boolean
  rowConfig?: Partial<RowProps>
  footerLayout?: Partial<ColProps>
  colLayout?: Partial<ColProps>
  itemStyle?: AnyCss
  rules?: FormRules
  hideItems?: Ref<string[]> | string[]
  propKey?: (string | number)[] | string | number
  isEditByRow?: boolean
}

export type allDataItem = {
  options: any[]
  optionAt: {
    [key: string]: any
  }
  value: any
  label: any
}

export interface allDataType {
  allRefs: Record<string, any>
  allFormItemRefs: Record<string, any>
  allEvent: Record<string, boolean>
  // 所有的动态信息
  allChangeData: Record<string, Record<string, allDataItem>>
  tableInfo?: VxeGridProps & {
    data: Record<string, any>[];
    column: ColumnInfo;
    slotInfo?: Record<string, any>
  }
}
